home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / fly8111-.000 / fly8111- / fly8 / util.c < prev    next >
C/C++ Source or Header  |  1979-12-31  |  7KB  |  415 lines

  1. /* --------------------------------- util.c --------------------------------- */
  2.  
  3. /* This is part of the flight simulator 'fly8'.
  4.  * Author: Eyal Lebedinsky (eyal@ise.canberra.edu.au).
  5. */
  6.  
  7. /* General purpose utility functions.
  8. */
  9.  
  10. #include "fly.h"
  11.  
  12.  
  13. extern int FAR
  14. opt36 (int c)
  15. {
  16.     if (c >= '0' && c <= '9')
  17.         return (c - '0');
  18.     if (c >= 'a' && c <= 'z')    /* Warning: ASCII only !!! */
  19.         return (c - 'a' + 10);
  20.     if (c >= 'A' && c <= 'Z')
  21.         return (c - 'A' + 10);
  22.     return (-1);
  23. }
  24.  
  25. extern int FAR
  26. get_long (char *p, long *lp)
  27. {
  28.     long    l;
  29.  
  30.     if (!p)
  31.         return (1);
  32.  
  33.     if (p[0] == '0') {
  34.         if (p[1] == 'x' || p[1] == 'X')
  35.             if (1 != sscanf (p+2, "%lx", &l))
  36.                 return (1);
  37.             else
  38.                 {}
  39.         else if (1 != sscanf (p, "%lo", &l))
  40.             return (1);
  41.         else
  42.             {}
  43.     } else if (1 != sscanf (p, "%ld", &l))
  44.         return (1);
  45.     else
  46.         {}
  47.     *lp = l;
  48.     return (0);
  49. }
  50.  
  51. extern int FAR
  52. get_int (char *p, int *li)
  53. {
  54.     long    l;
  55.  
  56.     if (get_long (p, &l) || l != (long)(int)l)
  57.         return (1);
  58.     if (l != (long)(int)l)
  59.         return (1);
  60.     *li = (int)l;
  61.     return (0);
  62. }
  63.  
  64. /* The following functions are used by the drivers to retrieve user options.
  65.  *
  66.  * The general format is ":[opt]:[opt]:[opt]..."
  67.  *    un-specified options are left out. Trailing ':::' not needed.
  68.  * an option is either "NameValue" or "Value". The first two functions
  69.  * use the Name and return the Value (either a pointer[non-terminated] or
  70.  * a long) while the last two (get_i*) return the Value for the i-th (first
  71.  * is zero) option (no Name expected). NULL/-1L pointer means not present.
  72.  *    get_arg()    returns a pointer to the option Value (by name)
  73.  *    get_iarg()    returns a pointer to the option Value (by index)
  74.  *    get_parg()    returns a (string) copy of the option Value (by name)
  75.  *    get_piarg()    returns a (string) copy of the option Value (by index)
  76.  *    get_narg()    returns the Value as a long (by name)
  77.  *    get_niarg()    returnd the Value as a long (by index)
  78. */
  79.  
  80. extern char * FAR
  81. get_arg (char *options, char *argname)
  82. {
  83.     char    *p;
  84.     int    len;
  85.  
  86.     len = strlen (argname);
  87.  
  88.     for (p = options; p; p = strchr (p, ':')) {
  89.         ++p;
  90.         if (!strnicmp (p, argname, len)) {
  91.             p += len;
  92.             break;
  93.         }
  94.     }
  95.     return (p);
  96. }
  97.  
  98. extern char * FAR
  99. get_iarg (char *options, int argno)
  100. {
  101.     char    *p;
  102.     int    i;
  103.  
  104.     for (i = 0, p = options; p; p = strchr (p, ':'), ++i) {
  105.         ++p;
  106.         if (i == argno)
  107.             break;
  108.     }
  109.     return (p);
  110. }
  111.  
  112. extern char * FAR
  113. get_parg (char *options, char *argname)
  114. {
  115.     char    *p, *q;
  116.     int    t;
  117.  
  118.     if (F(p = get_arg (options, argname)))
  119.         return (0);
  120.     q = strchr (p, ':');
  121.     if (!q)
  122.         p = STRdup (p);
  123.     else {
  124.         t = *q;
  125.         *q = '\0';
  126.         p = STRdup (p);
  127.         *q = (char)t;
  128.     }
  129.     return (p);
  130. }
  131.  
  132. extern char * FAR
  133. get_piarg (char *options, int argno)
  134. {
  135.     char    *p, *q;
  136.     int    t;
  137.  
  138.     if (F(p = get_iarg (options, argno)))
  139.         return (0);
  140.     q = strchr (p, ':');
  141.     if (!q)
  142.         p = STRdup (p);
  143.     else {
  144.         t = *q;
  145.         *q = '\0';
  146.         p = STRdup (p);
  147.         *q = (char)t;
  148.     }
  149.     return (p);
  150. }
  151.  
  152. extern int FAR
  153. get_narg (char *options, char *argname, long *lp)
  154. {
  155.     char    *p;
  156.     long    l;
  157.  
  158.     if (F(p = get_arg (options, argname)) || get_long (p, &l))
  159.         return (1);
  160.     *lp = l;
  161.     return (0);
  162. }
  163.  
  164. extern int FAR
  165. get_niarg (char *options, int argno, long *lp)
  166. {
  167.     char    *p;
  168.     long    l;
  169.  
  170.     if (F(p = get_iarg (options, argno)) || get_long (p, &l))
  171.         return (1);
  172.     *lp = l;
  173.     return (0);
  174. }
  175.  
  176. extern int FAR            /* debug utility */
  177. looping (int i)
  178. {
  179.     static int    n;
  180.  
  181.     if (!i)
  182.         n = 0;
  183.     else if (++n > i)
  184.         return (1);
  185.  
  186.     return (0);
  187. }
  188.  
  189. /* This one is from "Numerical Recipes in C"
  190. */
  191.  
  192. #define IA    16807
  193. #define IM    2147483647UL
  194. #define IQ    127773UL
  195. #define IR    2836
  196. #define IMASK    123459876UL
  197.  
  198. static long    rand_seed = 1;
  199.  
  200. extern int FAR
  201. Frand (void)
  202. {
  203.     long    k;
  204.  
  205.     rand_seed ^= IMASK;
  206.     k = rand_seed / IQ;
  207.     rand_seed = IA * (rand_seed - k * IQ) - IR * k;
  208.     if (rand_seed < 0)
  209.         rand_seed += IM;
  210.     rand_seed ^= IMASK;
  211.     return ((int)(0x07fff & rand_seed));
  212. }
  213. #undef IA
  214. #undef IM
  215. #undef IQ
  216. #undef IR
  217. #undef IMASK
  218.  
  219. extern void FAR
  220. Fsrand (Uint seed)
  221. {
  222.     rand_seed = seed;
  223. }
  224.  
  225. extern void FAR
  226. Frandomize (void)
  227. {
  228.     Fsrand (Tm->Hires ());
  229. }
  230.  
  231.  
  232. static char    FAR sline[4][40] = {{0}};
  233. static int    FAR iline = 0;
  234.  
  235. LOCAL_FUNC char * NEAR
  236. alloc_line (void)
  237. {
  238.     char    *line;
  239.  
  240.     line = sline[iline++];
  241.     if (iline >= rangeof(sline))
  242.         iline = 0;
  243.     return (line);
  244. }
  245.  
  246. LOCAL_FUNC Uchar * NEAR
  247. edit_l (Ulong n, char *s, Ulong f)
  248. {
  249. #if 0
  250.     int    m, k, u;
  251.     char    *line;
  252.  
  253.     line = alloc_line ();
  254.  
  255.     u = (int)(n % 1000);        n /= 1000;
  256.     k = (int)(n % 1000);        n /= 1000;
  257.     m = (int)(n % 1000);        n /= 1000;
  258.     if (n)
  259.         sprintf (line, "%s%u,%03u,%03u,%03u",
  260.                 s, (int)n, m, k, u);
  261.     else if (m)
  262.         sprintf (line, "%s%u,%03u,%03u",
  263.                 s, m, k, u);
  264.     else if (k)
  265.         sprintf (line, "%s%u,%03u",
  266.                 s, k, u);
  267.     else
  268.         sprintf (line, "%s%u",
  269.                 s, u);
  270. #else
  271.     int    i, j;
  272.     int    k[20];
  273.     char    *line;
  274.  
  275.     line = alloc_line ();
  276.  
  277. /* break number into digits
  278. */
  279.     for (i = 0; i < rangeof(k); ++i) {
  280.         k[i] = (int)(n % 10);
  281.         n /= 10;
  282.     }
  283.  
  284. /* multiply by factor
  285. */
  286.     if (f) {
  287.         n = 0;
  288.         for (i = 0; i < rangeof(k); ++i) {
  289.             n += k[i] * f;
  290.             k[i] = (int)(n % 10);
  291.             n /= 10;
  292.         }
  293.     }
  294.  
  295. /* Print prefix
  296. */
  297.     j = 0;
  298.     if (s) {
  299.         while (*s)
  300.             line[j++] = *s++;
  301.     }
  302.  
  303. /* Find highest digit
  304. */
  305.     for (i = rangeof(k); --i > 0 && 0 == k[i];)
  306.         ;
  307.  
  308. /* Print number
  309. */
  310.     for (; i >= 0; --i) {
  311.         line[j++] = (char)('0' + k[i]);
  312.         if (i && !(i%3))
  313.             line[j++] = ',';
  314.     }
  315.     line[j] = '\0';
  316. #endif
  317.     return ((Uchar *)line);
  318. }
  319.  
  320. extern Uchar * FAR
  321. show_l (long l)
  322. {
  323.     Ulong    n;
  324.     char    *s;
  325.  
  326.     if (l < 0) {
  327.         n = (Ulong)-l;
  328.         s = "-";
  329.     } else {
  330.         n = (Ulong)l;
  331.         s = "";
  332.     }
  333.  
  334.  
  335.     return (edit_l (n, s, 0L));
  336. }
  337.  
  338. extern Uchar * FAR
  339. show_ul (Ulong l)
  340. {
  341.     return (edit_l (l, NULL, 0L));
  342. }
  343.  
  344. extern Uchar * FAR
  345. show_ulf (Ulong l, Ulong f)
  346. {
  347.     return (edit_l (l, NULL, f));
  348. }
  349.  
  350. extern Uchar *FAR
  351. show_time (char *title, Ulong tt)
  352. {
  353.     int    tn, ss, mm, hh;
  354.     Ulong    t;
  355.     char    *line;
  356.  
  357.     line = alloc_line ();
  358.  
  359.     t = tt / 100;
  360.     tn = (int)(t % 10);        t /= 10;
  361.     ss = (int)(t % 60);        t /= 60;
  362.     mm = (int)(t % 60);        t /= 60;
  363.     hh = (int)(t % 24);        t /= 24;
  364.     if (t)
  365.         sprintf (line, "%s%u+%02u:%02u:%02u.%01u",
  366.                 title, (int)t, hh, mm, ss, tn);
  367.     else if (hh)
  368.         sprintf (line, "%s%02u:%02u:%02u.%01u",
  369.                 title, hh, mm, ss, tn);
  370.     else
  371.         sprintf (line, "%s%02u:%02u.%01u",
  372.                 title, mm, ss, tn);
  373.     return ((Uchar *)line);
  374. }
  375.  
  376. /* A set of functions to store (P) or retrieve (G) Big (B) or Little (L)
  377.  * endian, Words (w) or Longwords (l). Used in the low level networking
  378.  * drivers.
  379. */
  380.  
  381. extern Uint FAR
  382. ComGBw (Uchar *p)
  383. {
  384.     return (((Uint)p[0] << 8) + p[1]);
  385. }
  386.  
  387. extern Uint FAR
  388. ComGLw (Uchar *p)
  389. {
  390.     return (((Uint)p[1] << 8) + p[0]);
  391. }
  392.  
  393. extern void FAR
  394. ComPBw (Uchar *p, Uint w)
  395. {
  396.     p[0] = (Uchar)(0x0ff & (w >> 8));
  397.     p[1] = (Uchar)(0x0ff & (w     ));
  398. }
  399.  
  400. extern void FAR
  401. ComPLw (Uchar *p, Uint w)
  402. {
  403.     p[0] = (Uchar)(0x0ff & (w     ));
  404.     p[1] = (Uchar)(0x0ff & (w >> 8));
  405. }
  406.  
  407. extern void FAR
  408. ComPBl (Uchar *p, Ulong l)
  409. {
  410.     p[0] = (Uchar)(0x0ff & (l >> 24));
  411.     p[1] = (Uchar)(0x0ff & (l >> 16));
  412.     p[2] = (Uchar)(0x0ff & (l >>  8));
  413.     p[3] = (Uchar)(0x0ff & (l      ));
  414. }
  415.